package com.example.administrator.easy.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.easy.R;
import com.example.administrator.easy.adapter.GuangFxHeadVpAdapter;
import com.example.administrator.easy.adapter.GuangFxLvAdapter;
import com.example.administrator.easy.config.NetConfig;
import com.example.administrator.easy.model.GuangFxHeadResult;
import com.example.administrator.easy.model.GuangFxHeadRoot;
import com.example.administrator.easy.model.GuangFxResult;
import com.example.administrator.easy.model.GuangFxRoot;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/14.
 */

public class GuangFxFragment extends Fragment implements AbsListView.OnScrollListener {
    private SwipeRefreshLayout swipe;
    private ListView lv;



    private GuangFxHeadVpAdapter vpAdapter;//头布局的adapter
    private GuangFxLvAdapter adapter;

    private ImageView[] iv;

    private ArrayList<GuangFxResult> list;

    private View headView;
    private ViewPager vp;//头布局的vp
    private ImageOptions options;
    private String d = "00";
    private boolean isLoad;
    private   GuangFxRoot gr;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guang_fx,null);
        initView(view);
        initData();
        setHeadView();
        setListener();
        return view;
    }

    private void setListener() {
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //下拉刷新
                d = "00";
                setHeadView();
            }
        });

        lv.setOnScrollListener(this);

        //?
        lv.addHeaderView(headView);
    }

    private void setData(String d) {//
        Log.e("MainsetData","ok");
        //
        if(d.equals("00")){
            list.clear();
        }
        Log.e("Mainnnnnnnnn",d);
        x.http().get(new RequestParams(NetConfig.FAXIAN +d), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("Mainfxlv",result);
                gr = new Gson().fromJson(result,GuangFxRoot.class);
                for(int i=0;i<gr.getResult().size();i++){
                    list.add(gr.getResult().get(i));
                }
                adapter.notifyDataSetChanged();


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("MainsetData","onerror");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }


    private void initData() {
        list = new ArrayList<>();
        adapter = new GuangFxLvAdapter(getActivity(),list);
        lv.setAdapter(adapter);

    }

    private void initView(View view) {
        swipe = (SwipeRefreshLayout) view.findViewById(R.id.sw);
        lv = (ListView) view.findViewById(R.id.fx_head_lv);
    }

    //加载头视图
    private void setHeadView(){
        Log.e("Mainhead","headView");
        headView = View.inflate(getActivity(),R.layout.fragment_guang_fx_item_head,null);
        //设置头布局

        vp = (ViewPager) headView.findViewById(R.id.guang_fx_head_vp);
        options = new ImageOptions.Builder().setUseMemCache(true).build();
        list.clear();
        x.http().get(new RequestParams(NetConfig.LUNBO), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("Mainfxonsuccess",result);
                GuangFxHeadRoot gr = new Gson().fromJson(result,GuangFxHeadRoot.class);
                int size = gr.getResult().size();//图片个数
                Log.e("Mainsize",size+"");//11
               // ImageView[] iv = new ImageView[size];
                ArrayList<View> fList = new ArrayList<View>();
                for(int i=0;i<size;i++){
                    View v = View.inflate(getActivity(),R.layout.fragment_image,null);
                    ImageView iv = (ImageView) v.findViewById(R.id.image);
                    x.image().bind(iv,gr.getResult().get(i).getImg(),options);
                    fList.add(v);
                }

                if(vpAdapter==null){
                    vpAdapter = new GuangFxHeadVpAdapter(getActivity(), fList);
                    vp.setAdapter(vpAdapter);
                }else{
                    vpAdapter.notifyDataSetChanged();
                }


                //加载下方Listveiw
                setData(d);
                //停止刷新
                swipe.setRefreshing(false);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("Mainfx","onError");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if(isLoad&&AbsListView.OnScrollListener.SCROLL_STATE_IDLE==scrollState){
            Toast.makeText(getActivity(),"正在加载下一页",Toast.LENGTH_LONG).show();
            String jie = list.get(gr.getResult().size()-1).getPublishDate();
            d = jie.substring(6,19);
            Log.e("Mainnnnnnnnnnnd",d);
            setData(d);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//        if(firstVisibleItem+visibleItemCount==totalItemCount){
//
//            isLoad = true;
//
//        }
        isLoad = (firstVisibleItem+visibleItemCount)==totalItemCount;
        Log.e("Mainnnn",isLoad+"");
    }
}
